package com.lemon.rpcframe.core;

import java.util.Date;

import net.sf.cglib.beans.BeanCopier;

import org.apache.log4j.Logger;

import com.lemon.rpcframe.dao.entity.TaskDefineBean;
import com.lemon.rpcframe.dao.entity.TaskExecutableBean;
import com.lemon.rpcframe.dao.entity.TaskExecutingBean;
import com.lemon.rpcframe.dao.entity.TaskLogBean;

/**
 * 
 *
 *
 * @author wangyazhou
 * @version 1.0
 * @date  2015年9月14日 上午11:45:19
 * @see 
 * @since
 */
public class BeanConverter {

    private static final Logger logger = Logger.getLogger(BeanConverter.class);

    public static final BeanCopier executingLogCopier = BeanCopier.create(TaskExecutingBean.class, TaskLogBean.class, false);

    public static final BeanCopier executingExecutableCopier = BeanCopier.create(TaskExecutingBean.class, TaskExecutableBean.class, false);

    public static final BeanCopier executableExecutingCopier = BeanCopier.create(TaskExecutableBean.class, TaskExecutingBean.class, false);

    public static final BeanCopier defineExecutableCopier = BeanCopier.create(TaskDefineBean.class, TaskExecutableBean.class, false);

    public static TaskLogBean convertExecutingToTaskLog(TaskExecutingBean taskExecuting) {
        TaskLogBean logbean = new TaskLogBean();
        executingLogCopier.copy(taskExecuting, logbean, null);
        logbean.setStartTime(new Date());
        logger.info("convert executing to tasklog");
        return logbean;
    }

    public static TaskExecutableBean convertExectuingToExecutable(TaskExecutingBean taskExecuting) {
        TaskExecutableBean taskExecutable = new TaskExecutableBean();
        executingExecutableCopier.copy(taskExecuting, taskExecutable, null);

        logger.info("convert executing to executable");
        return taskExecutable;
    }

    public static TaskExecutingBean executableToExecutng(TaskExecutableBean task) {
        TaskExecutingBean executing = new TaskExecutingBean();
        executableExecutingCopier.copy(task, executing, null);
        return executing;
    }

    public static TaskExecutableBean defineToExecutable(TaskDefineBean task) {
        TaskExecutableBean executable = new TaskExecutableBean();
        defineExecutableCopier.copy(task, executable, null);
        return executable;
    }
}
